package com.taotao.netty.socket.oneclient.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 客户端自定义处理器
 * @Author: TaoJF
 * @Date: Created in 2018/6/28 上午12:05
 */
public class MyClientHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        TimeUnit.SECONDS.sleep(1);
        System.out.println(ctx.channel().remoteAddress());
        System.out.println("client output：" + msg);
        ctx.writeAndFlush("from client：" + LocalDateTime.now());
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush("来自客户端的问候~");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
